package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.mapper.OrderDetailMapper;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.CartService;
import com.itheima.reggie.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private CartService cartService;
    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public void save(Order order) {
        User user = UserHolder.get();
        List<Cart> cartList = cartService.cartList();
        if (CollectionUtil.isEmpty(cartList)) {
            throw new CustomException("购物车为空，无法生成订单");
        }
        Address address = addressMapper.selectById(order.getAddressId());
        if (address ==null){
            throw new CustomException("收货地址不存在,无法生成订单");
        }
        long id = IdWorker.getId();
        BigDecimal tomal = new BigDecimal(0);
        for (Cart cart : cartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(cart.getName());
            orderDetail.setOrderId(id);//当前明细所属订单的id
            orderDetail.setDishId(cart.getDishId());
            orderDetail.setSetmealId(cart.getSetmealId());
            orderDetail.setDishFlavor(cart.getDishFlavor());
            orderDetail.setNumber(cart.getNumber());
            orderDetail.setAmount(cart.getAmount());
            orderDetail.setImage(cart.getImage());
            orderDetailMapper.insert(orderDetail);
            tomal=tomal.add(orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber())));
        }
        order.setId(id);
        order.setNumber(id+"");
        order.setStatus(2);
        order.setUserId(user.getId());
        order.setOrderTime(new Date());
        order.setCheckoutTime(new Date());
        order.setAmount(tomal);
        order.setUserName(user.getName());
        order.setPhone(address.getPhone());
        order.setAddress(address.getDetail());
        order.setConsignee(address.getConsignee());
        orderMapper.insert(order);

        cartService.deleted();
    }

    @Override
    public Page<Order> findByPage(Integer pageNum, Integer pageSize) {
        Page<Order> page = new Page<>(pageNum, pageSize);
        User user = UserHolder.get();
        LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Order::getOrderTime);
        wrapper.eq(Order::getUserId,user.getId());
        page = orderMapper.selectPage(page,wrapper);
        List<Order> orderList = page.getRecords();
        for (Order order : orderList) {
            LambdaQueryWrapper<OrderDetail> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(OrderDetail::getOrderId,order.getId());
            List<OrderDetail> orderDetails = orderDetailMapper.selectList(wrapper1);
            order.setOrderDetails(orderDetails);
        }


        return page;
    }

    @Override
    public Page<Order> findAll(Integer pageNum, Integer pageSize,String number, String beginTime, String endTime) {
            Page<Order> page = new Page<>(pageNum, pageSize);
            LambdaQueryWrapper<Order> wrapper = new LambdaQueryWrapper<>();
            wrapper.like(number!=null,Order::getNumber,number)
                .gt(StringUtils.isNotEmpty(beginTime),Order::getOrderTime,beginTime)
                .lt(StringUtils.isNotEmpty(endTime),Order::getOrderTime,endTime);
            return orderMapper.selectPage(page,wrapper);

    }
}
